package org.example.algorithm;

public class IsSubsequenceSolution {

    public static void main(String[] args) {
        String s = "acb";
        String t = "ahbgdc";
        System.out.println(isSubsequence(s, t));
    }

    //方式一:使用嵌套循环
    public static boolean isSubsequence(String s, String t) {
        if (s == null || s.isEmpty()) {
            return true;
        }
        if (t == null || t.isEmpty()) {
            return false;
        }
        int j = 0;
        for (int i = 0; i < s.length(); i++) {
            if(j == t.length()) {
                return false;
            }
            for (; j < t.length(); j++) {
                if (s.charAt(i) == t.charAt(j)) {
                    j++;
                    break;
                }
                if(j == t.length() - 1) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isSubsequence2(String s, String t) {
        if (s == null || s.isEmpty()) {
            return true;
        }
        if (t == null || t.isEmpty()) {
            return false;
        }
        int i = 0;
        int j = 0;
        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;
            }
            j++;
        }
        return i == s.length();
    }
}
